package com.tevintao.marketingsystem.task;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.services.ViewOperation;
import com.tevintao.marketingsystem.xposted.HookWebView;
import com.tevintao.marketingsystem.xposted.WxTaskManger;

import java.util.Random;

import static com.tevintao.marketingsystem.task.WxUiViewConfig.getViewByClassName;
import static com.tevintao.marketingsystem.task.WxUiViewConfig.getViewByIdAndContent;


public class ForwardSubscribeTaskProcess extends SetTaskExtra {
    private static ForwardSubscribeTaskProcess ourInstance = new ForwardSubscribeTaskProcess();

    public static ForwardSubscribeTaskProcess getInstance() {
        return ourInstance;
    }

    private ForwardSubscribeTaskProcess() {
    }

    private Boolean ifPassNoLoaction = false;//是否按下  跳过按钮   使   无法定位提示框跳过
    private Boolean ifSelectSex = false;//是否已经选择了查看  男  女   或者全部
    private Boolean ifCall = false;//是否打过招呼了。是的话，返回附近人列表，没有的话就 接着打招呼
    private static Boolean ifsubscribe = false;//是否转发了公众号文章
    private Boolean ifpasehistory = false;//

    private int mSelectPosition = -1;
    private int ifclickmenu = 0;//是否需要点击右上角的菜单选项，（由于有多个不同的页面 ，都处于com-tencent-mm.plugin.webview.ui.tools.fts.FTSBizWebViewUI下边，此变量不同的数值可以保证每次进入它下边的时候，处理不同的页面）
    private Random mRandom = new Random();
    private static WebView webView = null;

    private static String mLastHtml = "";


    public static String testScript = "var alink =document.getElementsByTagName('a')[0] ; window.local_obj.showLink('html='+alink.innerHTML+'=html'); alink.click();  ";

    @SuppressWarnings("ResourceType")
    @Override
    public boolean processView(String contextName, View v) {
        //获取网页的HTML 代码    這個用於打印觀察  網頁結構   為自動點擊  webview裡邊的  某一個新聞條目做好準備
//        if (WxTaskProcess.DEBUG_MODEL) {
//            v.getContext().sendBroadcast(new Intent(HookWebView.ACTION_GET_LAST_HTML));
//            Object htmlObj = getExtra(HookWebView.KEY_LAST_HTML);
//            if (null != htmlObj) {
//                String html = htmlObj.toString();
////                LogManager.getInstance().logE("ttw", html);
//            }
//        } else {
//            HookWebView.getInstance().loadHtml();
//            LogManager.getInstance().logInfo("mLastHtml = " + HookWebView.mLastHtml);
//        }

        LogManager.getInstance().logInfo("ForwardSubscribeTaskProcess processView contextName = " + contextName + " v = " + v.toString());
        WxUiViewConfig.printViewGroup((ViewGroup) v);

        if (WxUiViewConfig.MAIN_LAUNCH.equals(contextName)) {
            boolean inContact = WxUiViewConfig.inTab(v, WxUiViewConfig.TAB_CONTACTS);
            LogManager.getInstance().logInfo("intab = " + inContact);
            if (inContact) {
                View SubscribeView = getViewByIdAndContent((ViewGroup) v, -1, "公众号");
                LogManager.getInstance().logInfo("SubscribeView = " + SubscribeView);
                if (null != SubscribeView) {
                    ViewOperation.clickView(SubscribeView);
                    ifPassNoLoaction = false;
                }
            }
            //mSelectPosition = -1;
        }
        else {
            if ("com.tencent.mm.plugin.brandservice.ui.BrandServiceIndexUI".equals(contextName)) {

                LogManager.getInstance().logInfo("当前位于公众号列表");
                View noneView = getViewByIdAndContent((ViewGroup) v,2131625218, "0个公众号");
                if(noneView!=null&&noneView.getVisibility()==View.VISIBLE)
                    taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "公众号列表为空  结束转发公众号文章  任务");
                View listView = v.findViewById(2131624537);
                LogManager.getInstance().logInfo("listView = " + listView);
                if (null != listView && listView instanceof ListView) {
                    ListView list = (ListView) listView;
                    if (mSelectPosition < 0) {
                        //（mSelectPosition的取值范围为1到list.getAdapter().getCount()-list.getHeaderViewsCount） 即是从第一个好友到最后一个好友
                        mSelectPosition = mRandom.nextInt(list.getAdapter().getCount() - 1) + list.getHeaderViewsCount();
                    }
                    int first = list.getFirstVisiblePosition();
                    int last = list.getLastVisiblePosition();
                    LogManager.getInstance().logInfo("mSelectPosition < 0 mSelectPosition = " + mSelectPosition + " first = " + first + " last =  " + last + " headerCount = " + list.getHeaderViewsCount() + list.getAdapter().getCount() + "  " + list.getCount() + " " + list.getFooterViewsCount());
                    if (mSelectPosition < first || mSelectPosition > last) {
                        ViewOperation.moveListPosition(list, mSelectPosition);
                        LogManager.getInstance().logInfo("不到位");
                        //WxUiViewConfig.printViewGroup((ViewGroup) v);
                    } else {
                        mSelectPosition = -1;
                        LogManager.getInstance().logInfo("移动到位");
                        View firstvisibleView = list.getChildAt(0);
                        if (null != firstvisibleView) {
                            ViewOperation.clickView(firstvisibleView);
                        }
                    }
                }
            } else if ("com.tencent.mm.ui.chatting.ChattingUI".equals(contextName)) {


                View selectMenu = getViewByClassName((ViewGroup) v, "ActionMenuItemView");
                if (null != selectMenu) {
                    ViewOperation.clickView(selectMenu);
                    LogManager.getInstance().logInfo("点击了右上角的菜单按钮");
                }
            } else if ("com.tencent.mm.plugin.profile.ui.ContactInfoUI".equals(contextName)) {
                LogManager.getInstance().logInfo("点击右上角的菜单按钮后    进入了公众号介绍页面");

                View lookmessageView = getViewByIdAndContent((ViewGroup) v, 16908310, "查看历史消息");
                //View sureView = v.findViewById(2131626642);
                if (null != lookmessageView) {
                    ViewOperation.clickView(lookmessageView);
                    LogManager.getInstance().logInfo("点击了查看历史消息按钮");
                } else {
                    View listview = v.findViewById(16908298);
                    LogManager.getInstance().logInfo("listView = " + listview);
                    if (null != listview && listview instanceof ListView) {
                        ListView list = (ListView) listview;
                        if (mSelectPosition < 0) {
                            //（mSelectPosition的取值范围为1到list.getAdapter().getCount()-list.getHeaderViewsCount） 即是从第一个好友到最后一个好友
                            mSelectPosition = mRandom.nextInt(list.getAdapter().getCount() - 1) + list.getHeaderViewsCount();
                        }
                        ViewOperation.moveListPosition(list, mSelectPosition);
                        LogManager.getInstance().logInfo("移动一次");
                    }
                    //lookmessageView = getViewByIdAndContent((ViewGroup) v, 16908310, "查看历史消息");
                    if (lookmessageView != null) {
                        ViewOperation.clickView(lookmessageView);
                        LogManager.getInstance().logInfo("点击了查看历史消息按钮");
                    }
                }
            } else if ("com.tencent.mm.plugin.webview.ui.tools.WebViewUI".equals(contextName)) {
                ifclickmenu++;
                LogManager.getInstance().logInfo("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
                //View titlehistoryView = getViewByIdAndContent((ViewGroup) v, 16908308, "查看历史消息");


                View titleview = v.findViewById(16908308);
                if (titleview instanceof TextView) {
                    TextView textView = (TextView) titleview;
                    if ("查看历史消息".equals(textView.getText().toString())) {
                        if (WxTaskProcess.DEBUG_MODEL) {
                            Object jsObj = getExtra(WxUiViewConfig.KEY_FORWARD_SUBSCRIBE_JAVA_SCRIBE);
                            LogManager.getInstance().logInfo("jsObj = " + jsObj);
                            if (null == jsObj) {
                                LogManager.getInstance().logError("js代码为空");
                                return true;
                            }
                            Intent intent = new Intent(HookWebView.ACTION_DO_JAVA_SCRIPT);
                            Bundle bundle = new Bundle();
                            bundle.putString(HookWebView.KEY_DO_JAVA_SCRIPT, jsObj.toString());
                            intent.putExtras(bundle);
                            v.getContext().sendBroadcast(intent);
                        } else {
                            Object jsObb = WxTaskManger.getInstance().getExtra(WxUiViewConfig.KEY_FORWARD_SUBSCRIBE_JAVA_SCRIBE);
                            if (null != jsObb)
                                HookWebView.getInstance().doScript(jsObb.toString());
                        }
                        ifpasehistory=true;
                    }
                    if (!"查看历史消息".equals(textView.getText().toString())) {
                        //TextView textView = (TextView) buttonViewFaSong;
                        LogManager.getInstance().logInfo("当前位于 公众号里  某一篇文章  里边");
//                        if(ifsubscribe==true) {
//                            LogManager.getInstance().logInfo("已经转发了  公众号文章  任务完成");
//                        }
                        if(ifpasehistory==true) {
                            View moreview = getViewByClassName((ViewGroup) v, "ActionMenuItemView");
                            if (null != moreview) {
                                ViewOperation.clickView(moreview);

                                LogManager.getInstance().logInfo("点击了右上角的 菜单按钮  准备转发到朋友圈");
                            }
                        }
                    }
                }
                View shareView = getViewByIdAndContent((ViewGroup) v, 2131624112, "分享到朋友圈");
                if (null != shareView) {
                    ViewOperation.clickView(shareView);
                    LogManager.getInstance().logInfo("点击了转发到朋友圈");
                }
            }
            else if ("com.tencent.mm.plugin.sns.ui.SnsUploadUI".equals(contextName)) {
                LogManager.getInstance().logInfo("当前位于   转发评论页面");
                //WxUiViewConfig.printViewGroup((ViewGroup) v);
                View editViewShuRu = v.findViewById(2131627762);
                if (editViewShuRu instanceof EditText) {
                    EditText textView = (EditText) editViewShuRu;
                    if ("".equals(textView.getText().toString())) {
                        //TextView textView = (TextView) buttonViewFaSong;
                        LogManager.getInstance().logInfo("等待自动输入评论内容 ");
                        String[] str = new String[]{
                                "好文章啊", "说的不错","好东西，就要拿来分享", "大家可以看看这个。", "很喜欢这篇文章", "有些订阅文章质量还是不错的", "学习一下咯。", "有意思", "好东西", "你们也看看呗。"
                        };
                        ViewOperation.setText(textView, str[mRandom.nextInt(10)]);
                    } else {
                        View enterView = v.findViewById(2131624139);

                        TextView enterbutton = (TextView) enterView;
                        if ("发送".equals(enterbutton.getText().toString())) {
                            ViewOperation.clickView(enterbutton);
                            LogManager.getInstance().logInfo("自动输入打招呼内容之后，按下了发送按钮");
                            taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "转发公众号文章到朋友圈成功");
                        }
                    }
                }

            }
        }
        return false;
    }
}
